/*
 * MIT License
 *
 * Copyright (c) 2021 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.seaboot.core;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * 测试函数
 *
 * 遍历文档内容
 *
 * <p>
 * IBodyElement：文档元素-段落和表格
 * XWPFParagraph：段落
 * XWPFTable：表格
 * XWPFStyles：样式
 * XWPFComment：批注
 * XWPFPicture ：图片
 * XWPFChart：图表
 * XWPFFooter：页脚
 * XWPFFootnote：脚注
 * XWPFHyperlink：代表超链接文本
 * XWPFSDT：代表 Structured Document Tag（SDT），IRunElement 子类，这是一种结构化文档标记，可以包含模板控件、元数据等。
 *
 * @author Mr.css
 * @version 2021-02-01 11:58
 */
public class Hello {


    public static void main(String[] args) throws IOException, XmlException {
        FileInputStream is = new FileInputStream("C:\\Users\\ASUS\\Desktop\\1.docx");
        try (XWPFDocument document = new XWPFDocument(is)) {

            Iterator<IBodyElement> iterator = document.getBodyElementsIterator();
            while (iterator.hasNext()) {
                IBodyElement element = iterator.next();
                if (element.getElementType() == BodyElementType.PARAGRAPH) {

                    // 处理段落
                    XWPFParagraph para = (XWPFParagraph) element;
                    for (IRunElement run : para.getIRuns()) {
                        System.out.println("XWPFSDT: " + (run instanceof XWPFSDT));
                    }

                    System.out.println(para.getClass());

                    // 段落信息
                    System.out.println("style: " + para.getStyle());
                    System.out.println("text: " + para.getText());

                    // ul 无序列表的角标样式
                    System.out.println("num_text: " + para.getNumLevelText());
                    // ul 嵌套级别
                    System.out.println("num_level: " + para.getNumIlvl());
                    // 引用的样式 ID
                    System.out.println("num_id：" + para.getNumID());

                    System.out.println(para.getCTP());

                    List<XWPFRun> runs = para.getRuns();
                    for (XWPFRun run : runs) {
                        System.out.println("color: " + run.getColor());
                        System.out.println("font-size: " + run.getFontSizeAsDouble());
                        System.out.println("font-name: " + run.getFontName());
                        System.out.println("font-family: " + run.getFontFamily());
                        System.out.println("lang: " + run.getLang());
                        System.out.println("text: " + run.text());

                        // 一般的文档用不上，都没内容
                        // System.out.println("style: " + run.getStyle());
                        // System.out.println("phonetic" + run.getPhonetic());
                        // System.out.println("kerning" + run.getKerning());
                        // System.out.println("mark" + run.getEmphasisMark());
                        // System.out.println("spacing" + run.getCharacterSpacing());
                        // System.out.println("font-name" + run.getFontName());
                        // System.out.println("alignment" + run.getVerticalAlignment());

                        // xml
                        // System.out.println(run.getCTR());

                        // 图片资源
                        List<XWPFPicture> pictures = run.getEmbeddedPictures();
                        System.out.println("images: " + pictures.size());
                        System.out.println("---------------------------------------------------------------");
                    }

                    System.out.println("===============================================================");
                } else if (element.getElementType() == BodyElementType.TABLE) {
                    // 处理表格
                    XWPFTable table = (XWPFTable) element;
                    // 跟 excel 的处理方式差别不多，就是各种遍历
                } else {
                    // 估计是啥高级应用，普通文档没这东西
                    System.out.println(BodyElementType.CONTENTCONTROL);
                }
            }

            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            System.out.println("~~~~~~~~~~~~~~[样式列表]~~~~~~~~~~~~~~~");

            // CTStyles 用于操作 xml 结构，精确控制文档的样式细节‌
            CTStyles ctStyles = document.getStyle();
            for (CTStyle style : ctStyles.getStyleList()) {
                System.out.println(style.getStyleId());
                System.out.println(style.getName());
                System.out.println(style.getPPr());
                System.out.println(style.getRPr());
            }

            // 与 CTStyles 功能基本一致，XWPFStyles 更侧重于提供高级 API
            XWPFStyles styles = document.getStyles();
            XWPFStyle style = styles.getStyle("3");
            CTStyle ctStyle = style.getCTStyle();
            System.out.println(ctStyle.getPPr());
            System.out.println(ctStyle.getRPr());
        }
        is.close();
    }
}
